﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;

namespace EffortsTracker
{
    class ScreenCaptureController
    {
        Session session;

        public ScreenCaptureController(Session session)
        {
            this.session = session;
        }

        public void captureScreen()
        {
            Rectangle bounds = Screen.GetBounds(Screen.GetBounds(Point.Empty));
            string extension = ".png";
            string ScreenPath = getFileName();

            if (ScreenPath != "")
            {
                extension = new FileInfo(ScreenPath).Extension;
            }
            Size curSize = Cursor.Current.Size;
            Point curPos = Cursor.Position;// new Point();
            
            CaptureImage(true, curSize, curPos,
                Point.Empty, Point.Empty, bounds, ScreenPath, extension);
        }

        private string getFileName()
        {
            string fileName = "image_";
            DateTime d = DateTime.Now;
            fileName += d.Year + "" + formatNum(d.Month) + "" + formatNum(d.Day) + "" + formatNum(d.Hour) + "" + formatNum(d.Minute) + "" + formatNum(d.Second);
            fileName += ".png";
            return fileName;
        }
        private string formatNum(int num)
        {
            if(num < 10)
                return "0" + num;
            return num + "";
        }

        public bool saveToClipboard = false;

        public void CaptureImage(bool showCursor, Size curSize, Point curPos, Point SourcePoint, Point DestinationPoint, Rectangle SelectionRectangle, string FilePath, string extension)
        {

            using (Bitmap bitmap = new Bitmap(SelectionRectangle.Width, SelectionRectangle.Height))
            {

                using (Graphics g = Graphics.FromImage(bitmap))
                {

                    g.CopyFromScreen(SourcePoint, DestinationPoint, SelectionRectangle.Size);

                    if (showCursor)
                    {

                        Rectangle cursorBounds = new Rectangle(curPos, curSize);
                        Cursors.Default.Draw(g, cursorBounds);

                    }

                }

                if (saveToClipboard)
                {

                    Image img = (Image)bitmap;
                    Clipboard.SetImage(img);

                }
                else
                {

                    switch (extension)
                    {
                        case ".bmp":
                            bitmap.Save(FilePath, ImageFormat.Bmp);
                            break;
                        case ".jpg":
                            bitmap.Save(FilePath, ImageFormat.Jpeg);
                            break;
                        case ".gif":
                            bitmap.Save(FilePath, ImageFormat.Gif);
                            break;
                        case ".tiff":
                            bitmap.Save(FilePath, ImageFormat.Tiff);
                            break;
                        case ".png":
                            bitmap.Save(FilePath, ImageFormat.Png);
                            session.addScreenShot(new ScreenShot((Image)bitmap, DateTime.Now));
                            break;
                        default:
                            bitmap.Save(FilePath, ImageFormat.Jpeg);
                            break;
                    }
                }
            }
        }
    }
}